package com.hyg.base.ui.base;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hyg.base.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

/**
 * FileName: RefreshListFragment
 * Author: HuangYuGuang
 * Date: 2021/3/24 11:51
 * Description: 可上拉刷新下拉加载更多的列表数据
 */
public abstract class RefreshListFragment<B,T extends BasePresenter> extends BaseFragment<T>{
    /*列表*/
    RecyclerView recyclerView;
    /*刷新*/
    SmartRefreshLayout refreshLayout;

    private BaseQuickAdapter<B, BaseViewHolder> baseAdapter;

    private int mPage = 1;
    private int mPageSize = 10;

    private View viewNoMoreData;
    private View viewLoadMore;
    private View viewLoading;

    /**
     * layout要扩展时子类重写这个方法
     */
    @Override
    protected int provideContentViewId() {
        return R.layout.comm_lay_refresh_recycler;
    }

    @Override
    protected void onFragmentFirstVisible() {
        initResreshView();
        initViewAndData();
        loadData(mPageSize,mPage=1);
    }

    protected abstract BaseQuickAdapter<B, BaseViewHolder> inflateBaseQuickAdapter();

    protected void onItemClick(BaseQuickAdapter adapter, View view, int position){}

    protected void onItemChildClick(BaseQuickAdapter adapter, View view, int position){}

    protected abstract void loadData(int pageSize,int page);

    protected void setPageSize(int pageSize) {
        this.mPageSize = pageSize;
    }

    protected B getItem(int position){
        return  baseAdapter.getItem(position);
    }

    protected List<B> getData(){
        return baseAdapter.getData();
    }

    protected void notifyDataSetChanged(){
        baseAdapter.notifyDataSetChanged();
    }

    protected void initResreshView(){
        recyclerView = findViewById(R.id.recyclerView);
        refreshLayout = findViewById(R.id.refreshLayout);
        viewNoMoreData = View.inflate(getActivity(),R.layout.layout_footer_end,null);
        viewLoadMore = View.inflate(getActivity(),R.layout.layout_footer_load_more,null);
        viewLoading = View.inflate(getActivity(),R.layout.layout_footer_loading,null);
        viewLoadMore.findViewById(R.id.view_load_more).setOnClickListener(v -> {
            baseAdapter.setFooterView(viewLoading);
            loadData(mPageSize,++mPage);
        });

        baseAdapter = inflateBaseQuickAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(baseAdapter);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(3000);
                refreshLayout.setEnableLoadMore(true);
                hideEmptyView();
                loadData(mPageSize,mPage=1);
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(3000);
                loadData(mPageSize,++mPage);
            }
        });
        baseAdapter.setOnItemClickListener((adapter, view1, position) -> {
            onItemClick(adapter,view1,position);
        });
        baseAdapter.setOnItemChildClickListener((adapter, view12, position) -> {
            onItemChildClick(adapter,view12,position);
        });
    }

    /**
     *  网络请求成功后调用
     * @param bean 接口返回的列表数据
     */
    protected  void onNextData(List<B> bean){
        if(mPage==1){
            refreshLayout.finishRefresh();
            if(bean==null || bean.size()==0){
                showEmptyView();
            }else {
                baseAdapter.setNewData(bean);
                if(bean.size()<mPageSize){
                    setNoMoreFootView();
                }else {
                    baseAdapter.setFooterView(viewLoadMore);
                }
            }
        }else {
            refreshLayout.finishLoadMore();
            if(bean!=null && bean.size()>0){
                baseAdapter.addData(bean);
            }
            if(bean==null || bean.size()<mPageSize){
                setNoMoreFootView();
            }else {
                baseAdapter.setFooterView(viewLoadMore);
            }
        }
    }

    protected void stopRefresh(){
        baseAdapter.setFooterView(viewLoadMore);
    }

    private void setNoMoreFootView(){
        baseAdapter.setFooterView(viewNoMoreData);
        refreshLayout.setEnableLoadMore(false);
    }

    /**
     * 无数据的页面显示，如果需要可以重新该方法
     */
    protected void showEmptyView(){
        mStateView.showEmpty();
    }

    protected void hideEmptyView(){
        mStateView.showContent();
    }
}
